<?php
/**
 * Author: liqinglin
 * CreateTime: 2018-12-26 16:07
 * description:
 */

namespace app\admin\controller;

use app\admin\model\Dynamic as DynamicModel;
use app\admin\model\DynamicErrorMessage;
use think\Db;

class Dynamic extends Base
{

    /*public function __construct()
    {
        parent::__construct();
        //验证token 是否正确；
        $this->validationToken(\think\Request::instance()->header('token'));
    }*/

    public function Dynamiclist()
    {

        $s_status = $this->request->param('status', 0);
        $keyword = $this->request->param('keyword');
        $service_id = $this->request->param('service_id');
        $limit = $this->request->param('limit', 10);
        $search['status'] = $s_status;
        $search['keyword'] = $keyword;
        $search['service_id'] = $service_id;

        $dynamicQuery = Db::name('cn_service_dynamic')
            ->where('status', $s_status)
            ->order('create_time', 'desc')
            ->field('dynamic_id,service_id,title,show_image,create_time,update_time,status,error_message,comment_number,look_number');
        if (!empty($keyword)) {
            $dynamicQuery = $dynamicQuery->where(" title LIKE '%{$keyword}%' OR  content LIKE  '%{$keyword}%'");
        }
        if (!empty($service_id)) {
            $dynamicQuery = $dynamicQuery->where("service_id", $service_id);
        }
        $data = $dynamicQuery->paginate($limit)->appends($search);
        return view('/dynamic/list', compact('data', 'search'));
    }

    public function DynamicDetail()
    {
        $dynamic_id = $this->request->param('dynamic_id');
        if (empty($dynamic_id)) {
            return json(array('status' => 30000, 'msg' => lang('缺少必要参数')));
        }
        $data = Db::name('cn_service_dynamic')
            ->where('dynamic_id', $dynamic_id)
            ->field('dynamic_id,content,author,title,show_image,forward_show_image,create_time,update_time,status')
            ->find();
        $data['error_message'] = Db::name('cn_service_dynamic_error_message')
            ->where('dynamic_id', $dynamic_id)
            ->where('is_del', 0)
            ->field('message')
            ->select();
        return view('/dynamic/detail', compact('data', 'search'));
    }

    /**
     * @api {post} /serviceadmin/Dynamic/delServiceDynamic 删除服务商动态
     * @apiVersion 1.0.0
     * @apiGroup Dynamic
     * @apiPermission ALL
     *
     * @apiDescription 删除服务商动态
     * @apiHeader  {string} token
     * @apiParam {string} [request_type] 请求来源('h5','android','ios','pc')
     * @apiParam {string} dynamic_id 服务商动态id
     *
     * @apiSuccessExample {json} 成功示例:
     * {"status":200,"msg":"成功"}
     */
    public function delServiceDynamic(\think\Request $request)
    {
        $dynamic_id = $this->request->param('dynamic_id', '');
        if (empty($dynamic_id)) {
            return json(array('status' => 30000, 'msg' => lang('缺少必要参数')));
        }
        $dynamic = DynamicModel::get($dynamic_id);
        if ($dynamic && $dynamic->status != 3) {
            $dynamic->status = 3;
            $dynamic->save();
            return json(array('status' => 200, 'msg' => lang('成功')));
        } else {
            return json(array('status' => 30003, 'msg' => lang('不存在该动态')));
        }
    }


    /**
     * @api {post} /serviceadmin/Dynamic/ExamineDynamic 进行审核服务商的动态
     * @apiVersion 1.0.0
     * @apiGroup Dynamic
     * @apiPermission ALL
     *
     * @apiDescription 进行审核服务商的动态
     * @apiHeader  {string} token
     * @apiParam {string} [request_type] 请求来源('h5','android','ios','pc')
     * @apiParam {int} status 审核状态 (2审核通过,4审核拒绝,)
     * @apiParam {int} dynamic_id 动态id
     * @apiParam {array} [error_message] 审核失败的原因 例如[{'value':'问题1'},{'value':'问题2}]
     *
     * @apiSuccessExample {json} 成功示例:
     *{"status":200,"msg":"成功","data":[]}
     */
    public function ExamineDynamic(\think\Request $request)
    {
        $postdata = $request->only('error_message,dynamic_id,status');
        $s_status = $postdata['status'];
        $dynamic_id = $postdata['dynamic_id'];
        $error_message = isset($postdata['error_message']) ? $postdata['error_message'] : '';
        if (empty($dynamic_id) || empty($s_status)) {
            return json(array('status' => 30000, 'msg' => lang('缺少必要参数')));
        }
        if (!in_array($s_status, [2, 4])) {
            return json(array('status' => 30002, 'msg' => lang('参数不是指定的范围')));
        }
        if ($s_status == 4 && empty($error_message) && !is_array($error_message)) {
            return json(array('status' => 30000, 'msg' => lang('缺少必要参数')));
        }
        $res = Db::name('cn_service_dynamic')
            ->where('dynamic_id', $dynamic_id)
            ->find();
        if (empty($res)) {
            return json(array('status' => 30003, 'msg' => lang('不存在该动态')));
        }

        if ($s_status == 4) {
            DynamicErrorMessage::saveErrorMessage($error_message, $dynamic_id);
        }
        Db::name('cn_service_dynamic')
            ->where('dynamic_id', $dynamic_id)
            ->update(['status' => $s_status]);
        $message_status = ($s_status == 2) ? 1 : 2;
        Db::name('cn_service_base')
            ->where('service_id', $res['service_id'])
            ->update(['message_status' => $message_status, 'is_news' => 1]);
        return json(array('status' => 200, 'msg' => lang('成功'), 'data' => []));
    }

    /**
     * @api {post} /serviceadmin/Dynamic/getOneDynamicInfo 获取服务商一个动态的信息
     * @apiVersion 1.0.0
     * @apiGroup Dynamic
     * @apiPermission ALL
     * @apiDescription 获取服务商一个草稿的信息
     * @apiHeader  {string} token
     * @apiParam  {string} dynamic_id 动态id
     * @apiParam {string} [request_type] 请求来源('h5','android','ios','pc')
     * @apiSuccessExample {json} 成功示例:
     *{"status":200,"msg":"成功","data":{"dynamic_id":"1546420317","content":"<div data-v-2fb70935=\"\">请在这里输sjdflk&nbsp;</div><div data-v-2fb70935=\"\">说的</div><div data-v-2fb70935=\"\">的d d</div><div data-v-2fb70935=\"\">&nbsp;d</div><div data-v-2fb70935=\"\">&nbsp;d</div><div data-v-2fb70935=\"\">&nbsp;</div><div data-v-2fb70935=\"\"><br></div><div data-v-2fb70935=\"\">的</div><div data-v-2fb70935=\"\"><br></div><div data-v-2fb70935=\"\">&nbsp;d</div><div data-v-2fb70935=\"\"><br></div><div data-v-2fb70935=\"\"><br></div><div data-v-2fb70935=\"\"><br></div><div data-v-2fb70935=\"\">的地方地方</div><div data-v-2fb70935=\"\"><br></div><div data-v-2fb70935=\"\"><br></div><div data-v-2fb70935=\"\">df&nbsp;</div><div data-v-2fb70935=\"\"><br></div><div data-v-2fb70935=\"\">fd好</div><div data-v-2fb70935=\"\">f的</div><div data-v-2fb70935=\"\">f</div><div data-v-2fb70935=\"\">d fdf&nbsp;</div><div data-v-2fb70935=\"\"><br></div><div data-v-2fb70935=\"\">fd</div><div data-v-2fb70935=\"\">&nbsp;</div><div data-v-2fb70935=\"\">f</div><div data-v-2fb70935=\"\">df&nbsp;</div><div data-v-2fb70935=\"\">fd入内容</div><p><br></p>","author":"搜索","title":"我是谁我来自哪里，我要去哪里","show_image":"http:\\/\\/www.file.com\\/image.php\\/media\\/20181226\\/d21333521151d5bb034bfb5aad3590af.png","forward_show_image":"http:\\/\\/www.file.com\\/image.php\\/media\\/20181226\\/d21333521151d5bb034bfb5aad3590af.png","create_time":1546420317,"update_time":1546506179,"status":2,"error_message":[{"message":"测试"},{"message":"测试3"}]}}
     * @apiSuccess {string} show_image 展示动态图片url
     * @apiSuccess {string} forward_show_image 转发封面url
     * @apiSuccess {string} content 动态内容
     * @apiSuccess {string} dynamic_id 动态id
     * @apiSuccess {string} title 标题
     * @apiSuccess {string} author 作者
     * @apiSuccess {object} error_message 错误信息列表
     */
    public function getOneDynamicInfo()
    {
        $dynamic_id = $this->request->param('dynamic_id');
        if (empty($dynamic_id)) {
            return json(array('status' => 30000, 'msg' => lang('缺少必要参数')));
        }
        $data = Db::name('cn_service_dynamic')
            ->where('dynamic_id', $dynamic_id)
            ->field('dynamic_id,content,author,title,show_image,forward_show_image,create_time,update_time,status')
            ->find();
        $data['error_message'] = Db::name('cn_service_dynamic_error_message')
            ->where('dynamic_id', $dynamic_id)
            ->where('is_del', 0)
            ->field('message')
            ->select();
        return json(array('status' => 200, 'msg' => lang('成功'), 'data' => $data));
    }

}